package cn.openea.perpartner.meily.web.controller;


import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.constant.enums.ExpressCompany;
import cn.openea.perpartner.meily.web.entity.LogisticsInfo;
import cn.openea.perpartner.meily.web.entity.dto.LogisticsDTOFactory;
import cn.openea.perpartner.meily.web.entity.vo.ExpressLogisticsEditVo;
import cn.openea.perpartner.meily.web.entity.vo.GeneralLogisticsVo;
import cn.openea.perpartner.meily.web.entity.vo.LogisticsSearchVo;
import cn.openea.perpartner.meily.web.entity.vo.ModuleLogisticsVo;
import cn.openea.perpartner.meily.web.service.logistics.LogisticsQuery;
import cn.openea.perpartner.meily.web.service.logistics.businessModule.LogisticsModlueHandler;
import cn.openea.perpartner.meily.web.service.logistics.businessModule.LogisticsModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 快递相关接口
 * @createTime 2023年05月10日 10:36:00
 */
@Api(tags = "快递接口")
@RestController
@RequestMapping("/express/query")
public class ExpressController extends BaseController {

    @Resource
    private LogisticsQuery logisticsQuery;
    @Resource
    private LogisticsDTOFactory logisticsDTOFactory;
    @Resource
    private LogisticsModlueHandler logisticsModlueHandler;


    @ApiOperation("物流查询-物流单号，手机号")
    @PostMapping("/general/logistics")
    public AjaxResult generalLogisticsQuery(@RequestBody @Valid GeneralLogisticsVo generalLogisticsVo) {
        LogisticsInfo logisticsInfo = logisticsQuery.findByCompanyAndCourierNumberAndPhone(
                generalLogisticsVo.getExpressCompany(),
                generalLogisticsVo.getCourierNumber(),
                generalLogisticsVo.getPhone());
        return AjaxResult.success(logisticsDTOFactory.toInfoDTO.apply(logisticsInfo));
    }

    @ApiOperation("实时物流查询-模块ID")
    @PostMapping("/realTime/logistics")
    public AjaxResult realTimeLogisticsQuery(@RequestBody @Valid ModuleLogisticsVo moduleLogisticsVo) {
        LogisticsModuleService service = logisticsModlueHandler.getService(moduleLogisticsVo.getLogisticsModule());
        LogisticsInfo logisticsInfo = service.findRealTimeLogistics(moduleLogisticsVo.getId());
        return AjaxResult.success(logisticsDTOFactory.toInfoDTO.apply(logisticsInfo));
    }

    @ApiOperation("分页查询物流信息")
    @GetMapping("/page/list")
    public AjaxResult pageLogisticsList(LogisticsSearchVo searchVo) {
        startPage();
        List<LogisticsInfo> logisticsInfoList;
        if (searchVo.isFortyEighthourData()) {
            logisticsInfoList = logisticsQuery.findFortyEightHourData(searchVo);
        } else {
            logisticsInfoList = logisticsQuery.findBySearchVo(searchVo);
        }
        TableDataInfo dataTable = getDataTable(logisticsInfoList);
        Map<String, Object> data = new HashMap<>();
        data.put("list", logisticsInfoList.parallelStream()
                .map(item -> logisticsDTOFactory.toInfoDTO.apply(item))
                .collect(Collectors.toList()));
        data.put("total", dataTable.getTotal());
        return AjaxResult.success(data);
    }

    @ApiOperation("物流状态统计")
    @GetMapping("/status/statistics")
    public AjaxResult logisticsStatusStatistics(LogisticsSearchVo searchVo) {
        Map<String, Integer> map = logisticsQuery.logisticsStatusStatistics(searchVo);
        map.put("FORTYEIGHT_HOUR_RECEIVING_DATA", logisticsQuery.findFortyEightHourData(searchVo).size());
        return AjaxResult.success(map);
    }

    @ApiOperation("物流信息更改")
    @PostMapping("/logistics/change")
    public AjaxResult logisticsChange(@RequestBody @Valid ExpressLogisticsEditVo editVo) {
        ExpressCompany expressCompany = ExpressCompany.getByCode(editVo.getExpressCompanyCode());
        if (expressCompany == null) {
            return AjaxResult.error("不支持此快递公司");
        }
        LogisticsModuleService service = logisticsModlueHandler.getService(editVo.getLogisticsModule());
        service.logisticsChange(expressCompany, editVo.getId(), editVo.getCourierNumber());
        return AjaxResult.success();
    }

    @ApiOperation("获取快递公司")
    @GetMapping("/company/list")
    public AjaxResult findExpressCompanyList() {
        List<Map<String, String>> companyList = new ArrayList<>();
        for (ExpressCompany value : ExpressCompany.values()) {
            Map<String, String> data = new HashMap<>(2);
            data.put(value.getCode(), value.getName());
            companyList.add(data);
        }
        return AjaxResult.success(companyList);
    }

    @ApiOperation("获取物流记录")
    @GetMapping("/logistics/list")
    public AjaxResult findLogistics(LogisticsSearchVo searchVo) {
        List<LogisticsInfo> logisticsInfoList = logisticsQuery.findBySearchVo(searchVo);
        return AjaxResult.success(logisticsInfoList
                .stream()
                .map(item -> logisticsDTOFactory.toInfoDTO.apply(item))
                .collect(Collectors.toList()));
    }

}
